﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Web.BBS;
using PowerCreator.LMS.BBS.Entity;
using PowerCreator.LMS.Common.Script;


public partial class sysadmin_BBSManager_AddBBSModule : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerBBSModuleInfo objManagerBBSModuleInfo = new ManagerBBSModuleInfo();
    ManagerBBS objManagerBBS = new ManagerBBS();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["UserName"] = GetLoginEntity.UserName;
            ViewState["UserID"] = GetLoginEntity.UserID;
            InitPage();

            BinderDDL();

            string DomainID = Request.QueryString["DomainID"].ToString();
            ViewState["DomainID"] = DomainID;
            ddl_Department.SelectedValue = DomainID;
        }
    }

    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddDepartment_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Dep_Name.Text = GetLanageValueByKey("lbl_AddDepartment_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Dep_Sort.Text = GetLanageValueByKey("lbl_AddDepartment_Sort", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Dep_Updep.Text = GetLanageValueByKey("lbl_AddDepartment_Updep", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Dep_Remark.Text = GetLanageValueByKey("lbl_AddDepartment_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_InfoTitle.Text = GetLanageValueByKey("lab_AddBBSModule_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_DomainFatherID.Text = GetLanageValueByKey("lab_AddBBSModule_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DomainContent.Text = GetLanageValueByKey("lbl_EditResourceInfo_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_DomainName.Text = GetLanageValueByKey("lab_AddBBSModule_ModuleName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    //检查页面数据
    private bool CheckPageData()
    {
        if (txt_DepartmentName.Text.Trim() == "")
        {
            Script.Alert(this, "", GetLanageValueByKey("msg_AddBBSModule_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        

        return true;
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;

        VODomain vo = new VODomain();
        vo.DomainContent = txt_Description.Text;
        vo.DomainFatherID = int.Parse(ddl_Department.SelectedValue.ToString());
        vo.DomainName = txt_DepartmentName.Text;

        vo.DomainLevel = int.Parse(ddl_Department.SelectedValue.ToString());
        vo.DomainCreatorName = ViewState["UserName"].ToString();
        vo.DomainCreatorID = int.Parse(ViewState["UserID"].ToString());
        vo.DomainCreateTime = DateTime.Now;



        if (objManagerBBS.CreateDomain(vo))
        {
            Script.Alert(this, "", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        else
        {
            Script.Alert(this, "", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }

    }

    //绑定分类信息
    private void BinderDDL()
    {
        string search = "";
        DataTable dt = objManagerBBS.QueryDomain(search);
        ArrayList list = new ArrayList();
        list = GetDepartmentTree(dt);
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("lab_AddBBSModule_TopModule", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        list.Insert(0, li);

        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "ManagerModule.aspx";
        Script.RedirectURL(url);
    }

    protected void rdbtnTypeDepartment_CheckedChanged(object sender, EventArgs e)
    {
        //plDepartment.Visible = true;
    }
    protected void rdbtnTypeClass_CheckedChanged(object sender, EventArgs e)
    {
        //plDepartment.Visible = false;
    }

    /// <summary>
    /// 获取树结构类型的部门信息，List中为Text和Value值，ListItem项
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public ArrayList GetDepartmentTree(DataTable dt)
    {
        ArrayList list = new ArrayList();
        InitTree(null, dt, "0", ref list);
        return list;
    }

    /// <summary>
    /// 递归初始化树结构，在ArrayList方式显示
    /// </summary>
    /// <param name="pNode">TreeNode节点</param>
    /// <param name="dt">部门数据</param>
    /// <param name="parentid">上一级部门ID</param>
    /// <param name="listt"></param>
    private void InitTree(TreeNode pNode, DataTable dt, string parentid, ref ArrayList listt)
    {
        if (dt.Rows.Count > 0)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[DomainFatherID]=" + parentid;
            foreach (DataRowView drv in dv)
            {
                TreeNode tempNode = new TreeNode();
                if (pNode == null)
                {
                    //填充根节点
                    tempNode.Text = drv["DomainName"].ToString() + "&nbsp;";
                    tempNode.ShowCheckBox = true;
                    tempNode.Value = drv["DomainID"].ToString();

                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["DomainName"].ToString();
                    li.Value = drv["DomainID"].ToString();

                    listt.Add(li);

                    InitTree(tempNode, dt, drv["DomainID"].ToString(), ref listt);
                }
                else
                {
                    tempNode.Text = drv["DomainName"].ToString();
                    tempNode.Value = drv["DomainID"].ToString();
                    pNode.ChildNodes.Add(tempNode);
                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["DomainName"].ToString();
                    li.Value = drv["DomainID"].ToString();

                    listt.Add(li);

                    InitTree(tempNode, dt, drv["DomainID"].ToString(), ref listt);
                }
            }
        }
    }
}
